/**
 * 
 */
package com.gmail.at.dengtooling;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.gmail.at.dengtooling.api.Listener;
import com.gmail.at.dengtooling.packets.KeyExchange;

/**
 * @author Deng Tooling
 * @version 1.0
 */
public class Session {
	private final static Logger LOGGER = Logger.getLogger("Session");
	private String host = "";
	private int port = 22;
	private Socket socket = null;
	private Listener listener;

	public Session(Connection connection) {
		// TODO Auto-generated constructor stub
		host = connection.getHost();
		port = connection.getPort();
		connectAndBind();
		connection.addSession(this);
	}

	public void connectAndBind() {
		try {
			socket = new Socket(host, port);
		} catch (UnknownHostException e) {
			LOGGER.warning(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			LOGGER.warning(e.getMessage());
			e.printStackTrace();
		}
	}

	public void sayHello() {
		// TODO Auto-generated method stub
		LOGGER.info("Say hello to server.");
		String msg = "SSH-2.0-PuTTY_Snapshot_2011_02_24:r9105\r\n";

		OutputStream out = null;
		InputStream in = null;

		try {
			byte[] packet = msg.getBytes("ISO-8859-1");
			byte[] buffer = new byte[1024];

			out = socket.getOutputStream();
			listener.onSendSayHello(packet);
			out.write(packet);

			in = socket.getInputStream();
			in.read(buffer);
			listener.onRecieveSayHello(buffer);
			LOGGER.log(Level.INFO, packet.toString());
			// LOGGER.log(Level.INFO, buffer.toString());
		} catch (IOException e) {
			LOGGER.warning(e.getMessage());
			e.printStackTrace();
		}

	}

	public String arrayToString(Object array, int len) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("[");
		for (int i = 0; i < len; i++) {
			if (i > 0)
				buffer.append(", ");
			if (array instanceof byte[])
				buffer.append(((byte[]) array)[i]);
		}
		buffer.append("]");
		return buffer.toString();
	}

	public void getKeyExchange() {
		// TODO Auto-generated method stub
		InputStream in = null;
		OutputStream out = null;

		try {
			Thread.sleep(500);
			byte[] buffer = new byte[1024];

			in = socket.getInputStream();
			in.read(buffer);
			listener.onReciveKeyExchange(new KeyExchange(buffer));

			out = socket.getOutputStream();
			listener.onSendKeyExchange(new KeyExchange(buffer));
			out.write(buffer);

		} catch (IOException e) {
			LOGGER.warning(e.getMessage());
			e.printStackTrace();
		} catch (InterruptedException e) {
			LOGGER.warning(e.getMessage());
			e.printStackTrace();
		}

	}

	/**
	 * Get Session Listener
	 * 
	 * @param listener
	 *            Session Listener
	 */
	public void setListener(Listener listener) {
		this.listener = listener;
	}

	/**
	 * Set Session Listener
	 * 
	 * @return Session Listener
	 */
	public Listener getListener() {
		return listener;
	}

	public void init() {
		// TODO Auto-generated method stub
		this.sayHello();
		this.getKeyExchange();
	}

}
